package main;

import commons.Job;
import commons.ThreadPool;
import commons.utils.UtilsCalcul;
import commons.utils.UtilsTime;
import gnu.trove.set.hash.TIntHashSet;
import java.util.Scanner;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;


public class MainTest {

	public static void main(String args[]) {
//		Scanner sc = new Scanner(System.in);
//		test();
//		sc.nextLine();
//		test();
//		sc.nextLine();
//		test();
//		sc.nextLine();
		
//		testThread_pool();
//		testThread_new();
//		testThread_pool();
//		testThread_new();
		
		TIntHashSet r;
		
	}

	static void test()
	{
		try{
			int NB_TEST = 10000000;
			double d=0;



			UtilsTime.startTimer("NATIVE");
			for (int i = 0; i < NB_TEST; i++) {
				d += Double.parseDouble("123456789.123");
				d -= Double.parseDouble("12345678.123456");
			}
			System.out.println(d);
			UtilsTime.stopTimer("NATIVE");
			System.out.println(UtilsTime.getStringDuree("NATIVE"));


			d=0;
			UtilsTime.startTimer("PERSO v1");
			for (int i = 0; i < NB_TEST; i++) {
				d += UtilsCalcul.parseDouble("123456789.123");
				d -= UtilsCalcul.parseDouble("12345678.123456");
			}
			System.out.println(d);
			UtilsTime.stopTimer("PERSO v1");
			System.out.println(UtilsTime.getStringDuree("PERSO v1"));


			d=0;
			UtilsTime.startTimer("PERSO v2");
			for (int i = 0; i < NB_TEST; i++) {
				d += UtilsCalcul.parseDouble_v2("123456789.123");
				d -= UtilsCalcul.parseDouble_v2("12345678.123456");
			}
			System.out.println(d);
			UtilsTime.stopTimer("PERSO v2");
			System.out.println(UtilsTime.getStringDuree("PERSO v2"));

		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}


	static void testThread_new() {
		
		try {
			MyThread[] tabTh= new MyThread[12];
			long debCalcul = System.nanoTime();
			for (int i = 0; i <100; i++) {

				for (int k = 0; k <12; k++) {
					tabTh[k] = new MyThread();
					tabTh[k].nom = "Thread_" + k;
				}
				for (int k = 0; k <12; k++)
					tabTh[k].start();
				for (int k = 0; k <12; k++)
					tabTh[k].join();
			}
			long finCalcul = System.nanoTime();
			System.out.println("---- FIN DU CALCUL : " + ( (double)(finCalcul-debCalcul) / 1000000.0) + " ms ----");
			
		} catch (InterruptedException ex) {}
		
	}

	
	static void testThread_pool() {

		MyJob[] tabJob= new MyJob[12];
		for (int i = 0; i <12; i++) {
			tabJob[i] = new MyJob();
			tabJob[i].nom = "Job_" + i;
		}
		
		
		ThreadPool pool = new ThreadPool(12);
		pool.start();

		long debCalcul = System.nanoTime();
		for (int i = 0; i <300; i++) {
			pool.giveJobs(tabJob);
		}
		long finCalcul = System.nanoTime();
		System.out.println("---- FIN DU CALCUL : " + ( (double)(finCalcul-debCalcul) / 1000000.0) + " ms ----");
		
		pool.release();
		
		System.out.println("Tous les job sont finis");
	}
	
	static class MyJob implements Job {
		public String nom;
		@Override
		public void executeJob() {
			for (int i = 0; i <5; i++) {
				//System.out.println(nom + " : " + i);
				try {	
					Thread.currentThread().sleep(3);
				} catch (InterruptedException ex) {}
			}
		}
		
	}
	
	
	static class MyThread extends Thread {
		public String nom;
		@Override
		public void run() {
//			System.out.println(nom + " : " + "DEBUT job");
			for (int i = 0; i <5; i++) {
				//System.out.println(nom + " : " + i);
				try {	
					Thread.currentThread().sleep(3);
				} catch (InterruptedException ex) {}
			}
//			System.out.println(nom + " : " + "FIN job");
		}
		
	}
	
}
